package com.j1ay.forum.controller;

import com.alibaba.fastjson.JSONObject;
import com.j1ay.forum.common.aop.LogAnnotation;
import com.j1ay.forum.common.cache.Cache;
import com.j1ay.forum.model.dto.params.AgreeParams;
import com.j1ay.forum.service.QuestionService;
import com.j1ay.forum.model.dto.params.PageParams;
import com.j1ay.forum.model.dto.params.QuestionParam;
import com.j1ay.forum.model.pojo.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;

/**
 * @Author J1ay
 * @Date 2021/12/14 21:29
 * @Description 问题业务
 */

// json数据进行交互

@RestController
@RequestMapping("api/question")

@Api(value="Question Controller",tags={"问题类接口"})
public class QuestionController {

    @Autowired
    private QuestionService questionService;


    @ApiOperation(value = "讨论区首页数据")
    @PostMapping("list")
    // 记录日志
    @LogAnnotation(module = "问题", operation = "获取问题列表")
    //@Cache(expire = 5 * 60 * 1000, name = "questionList")
    public Result listQuestion(@RequestBody PageParams pageParams) {
        return questionService.listQuestion(pageParams);
    }


    @ApiOperation(value = "讨论区首页 话题度热门")
    @GetMapping("hot")
    @Cache(expire = 5 * 60 * 1000, name = "hot_question")
    public Result hotQuestion() {
        int limit = 5;
        return questionService.hotQuestion(limit);
    }


    @ApiOperation(value = "最新讨论")
    @GetMapping("new")
    @Cache(expire = 5 * 60 * 1000, name = "new_question")
    public Result newQuestion() {
        int limit = 5;
        return questionService.newQuestion(limit);
    }

    @ApiOperation(value = "根据类型查看问题")
    @PostMapping("byType/{typeId}")
    public Result findQuestionByType(@PathVariable("typeId") Long typeId, @RequestBody PageParams pageParams) {
        return questionService.findQuestionByType(typeId, pageParams);
    }

    @ApiOperation(value = "根据问题Id查看问题")
    @PostMapping("one/{questionId}")
    public Result findQuestionById(@PathVariable("questionId") Long questionId) {
        return questionService.findQuestionById(questionId);
    }
    @ApiOperation(value = "点赞该问题")
    @PostMapping("praise/{questionId}")
    public Result toPraiseQuestion(@PathVariable("questionId") Long questionId) {
        return questionService.toPraiseQuestion(questionId);
    }

    @ApiOperation(value = "问题采纳该答案")
    @PostMapping("agree")
    public Result toAgreeAnswer(@RequestBody AgreeParams agreeParams) {
        return questionService.toAgreeAnswer(agreeParams);
    }

    @ApiOperation(value = "根据用户id获取他的问题列表")
    @PostMapping("my/{username}")
    public Result getUserQuestion(@PathVariable("username") String username) {
        return questionService.getUserQuestion(username);
    }

    @ApiOperation(value = "添加问题")
    @PostMapping("add")
    public Result addQuestion(@RequestBody QuestionParam questionParam) {
        return questionService.addQuestion(questionParam);
    }

    @ApiOperation("删除问题")
    @DeleteMapping("delete/{id}")
    public Result delete(@PathVariable("id") Long id){
        return questionService.delete(id);
    }

}
